// index.js
import { setStorage, clearStorage, getStorage, toast } from "../../utils/storage"
Page({
  data: {
    roles: [], // 下拉选项
    selectedRole: '', // 选中的值
    showRole: false,
    username: '',
    password: '',
    passwordType: 'password',
    passwordIcon: 'closed-eye',
    loading: false
  },
  onLoad() {
    let token = getStorage(wx.$TOKEN_NAME);
    if (token)
      wx.redirectTo({
        url: '/pages/squra/squra',
      })
  },
  login() {
    // this.setData({ showRole: true })
    //调用远程服务器获取用户openId，然后显示相应角色选择。
    wx.login({
      success: async (res) => {
        let { statusCode, data: { code, data, message } = {} } = await wx.$http.post("/quality/wxlogin", { code: res.code }).catch(e => e);
        if (statusCode === 200 && code === 200) {
          if (typeof data === "string") {//当前用户已经绑定身份，返回的是token
            setStorage(wx.$TOKEN_NAME, data);
            //获取当前登录用户角
            ({ statusCode, data: { code, data, message } } = await wx.$http.get("/quality/mgr/userRole").catch());
            if (statusCode === 200 && code === 200) {
              let { sysRoleVOList: roles } = data;
              roles = roles.map(x => ({ roleCode: x.roleCode, roleName: x.roleName }));
              if (roles.filter(x => x.roleCode === "STUDENT").length === 0) {//查询是否存在学生角色
                toast({ title: "当前系统版本仅支持学生登录" });
                removeStorage(wx.$TOKEN_NAME);
                return;
              }
              roles = roles.map(x => x.roleName);
              this.setData({ roles });
              setStorage(wx.$USER_NAME, data);//当前登录用户角色

              wx.redirectTo({
                url: '/pages/squra/squra',
              });
            }
            else
              toast({ title: "无效的登录账号身份" });
          }
          else {//获取当前用户的unionid，并进行跳转绑定身份。
            wx.navigateTo({
              url: '/pages/verify/verify?unionid=' + data.unionid,
            })
          }
        }
        else
          toast({ title: message ?? "网络异常，请重试" });
      },
      fail() {
        toast({ title: "获取身份凭证失败" });
      }
    })
  },
  onShareAppMessage() {
    return getApp().globalData.shareApp;
  },

  // cancelPickerRole() {
  //   this.setData({ showRole: false });
  // },
  // pickerRole(e) {
  //   setStorage("roleId", e.detail.index + 1);
  //   this.setData({ showRole: false });
  //   wx.redirectTo({
  //     url: '/pages/squra/squra',
  //   })
  // },

  // async login() {
  //   if (this.data.loading)
  //     return;
  //   if (this.data.username.trim().length < 3) {
  //     toast({ title: "请输入3-16用户名" });
  //     return;
  //   }
  //   if (this.data.password.length < 6 || this.data.password.length > 16) {
  //     toast({ title: "请输入6-16位密码" });
  //     return;
  //   }

  //   //远程传输数据登录。
  //   this.setData({ loading: true });
  //   let { statusCode, data: { code, data, message } } = await wx.$http.post("/quality/login", {
  //     account: this.data.username,
  //     password: this.data.password
  //   });
  //   this.setData({ loading: false });
  //   if (statusCode === 200 & code === 200) {//登录成功。
  //     setStorage(wx.$TOKEN_NAME, data);
  //     //获取当前登录用户角
  //     ({ statusCode, data: { code, data, message } } = await wx.$http.get("/quality/mgr/userRole").catch());
  //     if (statusCode === 200 && code === 200) {
  //       let { sysRoleVOList: roles } = data;
  //       roles = roles.map(x => ({ roleCode: x.roleCode, roleName: x.roleName }));
  //       if (roles.filter(x => x.roleCode === "STUDENT").length === 0) {//查询是否存在学生角色
  //         toast({ title: "当前系统版本仅支持学生登录" });
  //         clearStorage(wx.$TOKEN_NAME);
  //         return;
  //       }
  //       roles = roles.map(x => x.roleName);
  //       this.setData({ roles });
  //       setStorage(wx.$USER_NAME, data);//当前登录用户角色

  //       //判断当前登录用户是否需要修改密码
  //       if (data.firstLogin)
  //         wx.redirectTo({
  //           url: '/pages/password/password',
  //         });
  //       else//跳转至活动广场
  //         wx.redirectTo({
  //           url: '/pages/squra/squra',
  //         })
  //     }
  //     else
  //       toast({ title: "无效的登录账号身份" });
  //   }
  //   else
  //     toast({ title: message });
  // },
  // switchPassword() {
  //   let type = this.data.passwordType;
  //   let icon = this.data.passwordIcon;
  //   if (type === "password") {
  //     type = "text";
  //     icon = "eye-o";
  //   }
  //   else {
  //     type = "password";
  //     icon = "closed-eye";
  //   }
  //   this.setData({ passwordType: type, passwordIcon: icon });
  // }
})
